package projetjava;

import Composite.Dossier;
import Composite.Element;
import Composite.Photo;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;


public class ListCellRendererApercu implements ListCellRenderer {
         
    private JPanel panel;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JLabel texte;
    private JLabel image;
    private static ImageIcon representationDossier = new ImageIcon("dossier100.png");
    
    private static int tailleCaseX =129;
    private static int tailleCaseY = 143;
    
    private JList maJList;
    
    
    public ListCellRendererApercu(JList maJList) {
        
        this.maJList = maJList;
        
    }
    
     
    public Component getListCellRendererComponent(JList list,final Object value, int index, boolean isSelected, boolean cellHasFocus)  {
        
        panel = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();
        texte = new JLabel();
        image = new JLabel();
        
        panel2.setLayout(new BorderLayout());
        
        Element element =(Element)value;
        
        JLabel photoLabel = new JLabel();
        panel2.add(photoLabel);
        photoLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        
        if(element instanceof Dossier) photoLabel.setIcon(representationDossier);
        else if(element instanceof Photo){
        BufferedImage im = element.getApercu("petitesVignettes/");
        if(im!=null) photoLabel.setIcon(new ImageIcon(im));
        }
        

        
        texte.setText(element.toStringReduit());
        texte.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        panel.setToolTipText(element.toString());
        
        panel.setLayout(new BorderLayout());
        
        panel2.setPreferredSize(new Dimension(100,100));
        panel3.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
        
        
        panel3.add(texte);
        panel.setOpaque(true);
        panel.add(panel2,BorderLayout.NORTH);
        panel.add(panel3,BorderLayout.SOUTH);
        
        panel4.add(panel);
        
        texte.setOpaque(true);
        
        panel.setBackground(list.getBackground());
        panel2.setBackground(list.getBackground());
        panel3.setBackground(list.getBackground());
        panel4.setBackground(list.getBackground());
        
        
        if (isSelected) {
            panel2.setBorder(BorderFactory.createMatteBorder(3,3,3,3,list.getSelectionBackground()));
            texte.setForeground(Color.DARK_GRAY);
            texte.setBackground(list.getSelectionBackground());
        } else {
            panel2.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));  
            texte.setForeground(Color.DARK_GRAY);
            texte.setBackground(list.getBackground());
        }
        
        
        panel4.setPreferredSize(new Dimension(tailleCaseX,tailleCaseY));    
               
        
        return panel4;
        
    }
    
}

